var member;
$(function(){
	
	//请求订单信息
   oneOrder();

});
//订单详细
function oneOrder(){
	var url = rootPath + "/order/oneOrder"; 
	var result = CommnUtil.ajax(url,{"orderId":orderId},"json");
	var order = null; //订单信息
	var products = null; //商品集合
	if(result.resultSuccess == true){
		order = result.data;
		if(order){
			products = order.products;
		}
		var expressUrl ='https://m.kuaidi100.com/index_all.html?type='+order.expressName+'&postid=' + order.expressCode;

		var status='';
		var $progress = $(".or_main .number p");
		var $info = $(".or_main .info");
        var $progressStr ="";
		//设置订单号
		$(".order_details .or_main .listing .order_number .three").text(orderId);
		var $p=$(".order_details .or_main .listing  p");
		
		//商品ul
		var $goods = $(".order_details .or_main .listing .multiple .goods");
		var $mul_ = $(".order_details .or_main .listing .multiple .mul_");
		var $height = "height:" + 116*products.length +"px";
		var payStatus = order.payStatus;
		var statusDetail='';
		var payStatus = order.payStatus;
		var shipStatus = order.shipStatus;
		var isComment = order.isComment;
		var isValid = order.isValid;

		var pmtOrder =order.pmtOrder;//订单优惠
		var proType =order.proType;//优惠方式
		var pmtGoods =order.pmtGoods;
	    var proDetailMap =order.proDetailMap;//店铺促销活动
	//	alert($p.html());
	//	alert($info.html());
	//	alert($listing.html());
		var postcode = "";
		if(order.postCode){
			postcode= order.postCode;
		}else{
			postcode="";
		}
		var $infoStr='<div>详细信息</div>'
					+'<p>收货人：'+ order.memberName +'</p>'
					+'<p>收货地址：' + order.shipAddress+'</p>'
					+'<p>邮政编码：' + postcode + '</p>'
					+'<p>联系电话：'+ order.memberPhone+ '</p>';	
		//alert($infoStr);

		$info.html($infoStr);
		var orderCreatTime ='';
		if(order.orderCreatTime){
			orderCreatTime = order.orderCreatTime;
		}else{
			orderCreatTime ='';
		}
		var orderPayTime='';
		if(order.orderPayTime){
			orderPayTime = order.orderPayTime;
		}else{
			orderPayTime ='';
		}
		var orderShipTime='';
		if(order.orderShipTime){
			orderShipTime = order.orderShipTime;
		}else{
			orderShipTime ='';
		}
		var orderOverTime='';
		if(order.orderOverTime){
			orderOverTime = order.orderOverTime;
		}else{
			orderOverTime ='';
		}
		if(order.payStatus == '3' && shipStatus =='0' && isComment=='0' && isValid=='0'){
			statusDetail ='等待买家付款';
			$progressStr ='<div><img src="'+ rootPath +'/images/tj.png" alt="提交订单" />'
						 +'<p>'
					       +'<span>'+ orderCreatTime +'</span>'
				        +'</p></div> ';
			status='<div><div class="red">'+'等待买家付款'+'</div></div>';
			
		}else if((payStatus == '1' || payStatus =='6')  && shipStatus =='0' && isComment=='0' && isValid=='0'){
			statusDetail ='等待卖家发货';
			$progressStr ='<div><img src="'+ rootPath +'/images/tj2.png" alt="买家付款" />'
						 +'<p>'
					       +'<span>'+ orderCreatTime +'</span>'
					       +'<span>'+orderPayTime +'</span>'
				        +'</p></div>  ';	
			status='<div><div class="red">'+'等待卖家发货'+'</div></div>';
						
		}else if((payStatus == '1' || payStatus =='6') &&( shipStatus =='1' || shipStatus == '2')&& isComment=='0' && isValid=='0'){
			statusDetail ='卖家已发货';
			$progressStr ='<div><img src="'+ rootPath +'/images/tj3.png" alt="卖家发货" />'
			 +'<p>'
		       +'<span>'+ orderCreatTime +'</span>'
		       +'<span>'+orderPayTime +'</span>'
		       +'<span>'+ orderShipTime +'</span>'
	        +'</p> </div> ';	
			status='<div><div class="red">'+'卖家已发货'+'</div><a href="'+expressUrl+'" target="_blank">查看物流</a></div>';
			
		}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='0' && isValid=='0'){
			statusDetail ='交易成功';
			$progressStr ='<div><img src="'+ rootPath +'/images/tj4.png" alt="确认收货" />'
			 +'<p>'
		       +'<span>'+ orderCreatTime +'</span>'
		       +'<span>'+orderPayTime +'</span>'
		       +'<span>'+ orderShipTime +'</span>'
		       +'<span>'+ orderOverTime +'</span>'
	        +'</p></div>';
			status='<div><div class="red">'+'交易成功'+'</div><a  href="'+expressUrl+'" target="_blank">查看物流</a></div>';
		}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='1' && isValid=='0'){
			statusDetail ='交易成功';
			$progressStr ='<div><img src="'+ rootPath +'/images/tj5.png" alt="评价" />'
							 +'<p>'
						       +'<span>'+ orderCreatTime +'</span>'
						       +'<span>'+orderPayTime +'</span>'
						       +'<span>'+ orderShipTime +'</span>'
						       +'<span>'+ orderOverTime +'</span>'
							   +'<span>'+ orderOverTime +'</span>'
					        +'</p></div>';
			status='<div><div class="red">'+'交易成功'+'</div><a  href="'+expressUrl+'" target="_blank">查看物流</a></div>';
		
		}else if(payStatus == '3' && shipStatus =='0' && isComment=='0' && isValid=='1'){
			statusDetail ='交易关闭';
			if(order.payStatus == '3' && shipStatus =='0' && isComment=='0' ){
				$progressStr ='<div><img src="'+ rootPath +'/images/tj.png" alt="提交订单" />'
							 +'<p>'
						       +'<span>'+ orderCreatTime +'</span>'
					        +'</p></div> ';
				
			}else if((payStatus == '1' || payStatus =='6')  && shipStatus =='0' ){
				$progressStr ='<div><img src="'+ rootPath +'/images/tj2.png" alt="买家付款" />'
							 +'<p>'
						       +'<span>'+ orderCreatTime +'</span>'
						       +'<span>'+orderPayTime +'</span>'
					        +'</p></div>  ';	
							
			}else if((payStatus == '1' || payStatus =='6') &&( shipStatus =='1' || shipStatus == '2')){
				$progressStr ='<div><img src="'+ rootPath +'/images/tj3.png" alt="卖家发货" />'
				 +'<p>'
			       +'<span>'+ orderCreatTime +'</span>'
			       +'<span>'+orderPayTime +'</span>'
			       +'<span>'+ orderShipTime +'</span>'
		        +'</p> </div> ';	
			}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='0'){
				$progressStr ='<div><img src="'+ rootPath +'/images/tj4.png" alt="确认收货" />'
				 +'<p>'
			       +'<span>'+ orderCreatTime +'</span>'
			       +'<span>'+orderPayTime +'</span>'
			       +'<span>'+ orderShipTime +'</span>'
			       +'<span>'+ orderOverTime +'</span>'
		        +'</p></div>';
			}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='1'){
				$progressStr ='<div><img src="'+ rootPath +'/images/tj5.png" alt="评价" />'
								 +'<p>'
							       +'<span>'+ orderCreatTime +'</span>'
							       +'<span>'+orderPayTime +'</span>'
							       +'<span>'+ orderShipTime +'</span>'
							       +'<span>'+ orderOverTime +'</span>'
								   +'<span>'+ orderOverTime +'</span>'
						        +'</p></div>';
			
			}
			status='<div><div class="red">'+'交易关闭'+'</div>';
			
		}else{
			if(order.payStatus == '3' && shipStatus =='0'){
				statusDetail ='等待买家付款';
				$progressStr ='<div><img src="'+ rootPath +'/images/tj.png" alt="提交订单" />'
							 +'<p>'
						       +'<span>'+ orderCreatTime +'</span>'
					        +'</p></div> ';
				status='<div><div class="red">'+'等待买家付款'+'</div></div>';
				
			}else if((payStatus == '1' || payStatus =='6')  && shipStatus =='0'){
				statusDetail ='等待卖家发货';
				$progressStr ='<div><img src="'+ rootPath +'/images/tj2.png" alt="买家付款" />'
							 +'<p>'
						       +'<span>'+ orderCreatTime +'</span>'
						       +'<span>'+orderPayTime +'</span>'
					        +'</p></div>  ';	
				status='<div><div class="red">'+'等待卖家发货'+'</div></div>';
							
			}else if((payStatus == '1' || payStatus =='6') &&( shipStatus =='1' || shipStatus == '2')){
				statusDetail ='卖家已发货';
				$progressStr ='<div><img src="'+ rootPath +'/images/tj3.png" alt="卖家发货" />'
				 +'<p>'
			       +'<span>'+ orderCreatTime +'</span>'
			       +'<span>'+orderPayTime +'</span>'
			       +'<span>'+ orderShipTime +'</span>'
		        +'</p> </div> ';	
				status='<div><div class="red">'+'卖家已发货'+'</div><a href="'+expressUrl+'" target="_blank">查看物流</a></div>';
				
			}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5'){
				statusDetail ='交易成功';
				$progressStr ='<div><img src="'+ rootPath +'/images/tj4.png" alt="确认收货" />'
				 +'<p>'
			       +'<span>'+ orderCreatTime +'</span>'
			       +'<span>'+orderPayTime +'</span>'
			       +'<span>'+ orderShipTime +'</span>'
			       +'<span>'+ orderOverTime +'</span>'
		        +'</p></div>';
				status='<div><div class="red">'+'交易成功'+'</div><a  href="'+expressUrl+'" target="_blank">查看物流</a></div>';
			}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5'){
				statusDetail ='交易成功';
				$progressStr ='<div><img src="'+ rootPath +'/images/tj5.png" alt="评价" />'
								 +'<p>'
							       +'<span>'+ orderCreatTime +'</span>'
							       +'<span>'+orderPayTime +'</span>'
							       +'<span>'+ orderShipTime +'</span>'
							       +'<span>'+ orderOverTime +'</span>'
								   +'<span>'+ orderOverTime +'</span>'
						        +'</p></div>';
				status='<div><div class="red">'+'交易成功'+'</div><a  href="'+expressUrl+'" target="_blank">查看物流</a></div>';
			
			}else if(payStatus == '3' && shipStatus =='0' ){
				statusDetail ='交易关闭';
				if(order.payStatus == '3' && shipStatus =='0' && isComment=='0' ){
					$progressStr ='<div><img src="'+ rootPath +'/images/tj.png" alt="提交订单" />'
								 +'<p>'
							       +'<span>'+ orderCreatTime +'</span>'
						        +'</p></div> ';
					
				}else if((payStatus == '1' || payStatus =='6')  && shipStatus =='0' ){
					$progressStr ='<div><img src="'+ rootPath +'/images/tj2.png" alt="买家付款" />'
								 +'<p>'
							       +'<span>'+ orderCreatTime +'</span>'
							       +'<span>'+orderPayTime +'</span>'
						        +'</p></div>  ';	
								
				}else if((payStatus == '1' || payStatus =='6') &&( shipStatus =='1' || shipStatus == '2')){
					$progressStr ='<div><img src="'+ rootPath +'/images/tj3.png" alt="卖家发货" />'
					 +'<p>'
				       +'<span>'+ orderCreatTime +'</span>'
				       +'<span>'+orderPayTime +'</span>'
				       +'<span>'+ orderShipTime +'</span>'
			        +'</p> </div> ';	
				}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='0'){
					$progressStr ='<div><img src="'+ rootPath +'/images/tj4.png" alt="确认收货" />'
					 +'<p>'
				       +'<span>'+ orderCreatTime +'</span>'
				       +'<span>'+orderPayTime +'</span>'
				       +'<span>'+ orderShipTime +'</span>'
				       +'<span>'+ orderOverTime +'</span>'
			        +'</p></div>';
				}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='1'){
					$progressStr ='<div><img src="'+ rootPath +'/images/tj5.png" alt="评价" />'
									 +'<p>'
								       +'<span>'+ orderCreatTime +'</span>'
								       +'<span>'+orderPayTime +'</span>'
								       +'<span>'+ orderShipTime +'</span>'
								       +'<span>'+ orderOverTime +'</span>'
									   +'<span>'+ orderOverTime +'</span>'
							        +'</p></div>';
				
				}
				status='<div><div class="red">'+'交易关闭'+'</div>';
				
			}
		}
		$(".or_main .number span").text("当前状态："+statusDetail);
		$progress.after($progressStr);
		
		$.each(products,function(i,product){
			  var productDetailUrl = rootPath + "/goods/queryGoodsDetail?id=" + product.productId;
			  var goods_sku_info = '';
				if(product.goods_sku_info){
					goods_sku_info = product.goods_sku_info;
				}else{
					goods_sku_info ='无规格信息';
				}
			  var goodsSkus = goods_sku_info.split(",");
			   var skusStr = '';			   
			   $.each(goodsSkus,function(i,goodsSku){
				   if(goodsSku){
					   skusStr +=  '<span>'+ goodsSku +'</span>'  
				   }
             	});
			   var $unitPrice ="";
				if(product.discountMarketPrice){
					$unitPrice = product.discountMarketPrice;
				}else{
					$unitPrice = product.marketPrice;
				}
				$unitPrice= parseFloat($unitPrice).toFixed(2);
				var totalPrice = parseFloat($unitPrice)*parseFloat(product.quantity);
				var goodsPro = goodsProMoney(totalPrice,product.goodsProMap);//商品促销优惠	
			//totalPrice = (totalPrice+order.shipMoney).toFixed(2);
				totalPrice = totalPrice.toFixed(2);
                var $proDetail = '无优惠';
                if(!CommnUtil.notNull(goodsPro)){
                	$proDetail = "参与了" + goodsPro.proDetailTitle+'优惠活动，优惠了'+ goodsPro.discountamount.toFixed(2) +'元';
                }
                debugger;
			   var $listingStr = '<ul class="clearfix">'
				                    +'<li>'
				                        +'<div class="name clearfix">'
				                             +'<a href="'+ productDetailUrl +'"><img src="' + product.picture + '" alt="' + product.productName + '" /></a>'
				                             +'<div class="l">'
					                               +'<a href="'+ productDetailUrl +'" target="_blank">' + product.productName + '</a>'
					                               +'<p class="f12 nine">'+skusStr+'</p>'
				                             +'</div>'
				                         +'</div>'
				                         +'<div>'
			                             //    +'<s>￥298.00</s><br />'
			                                 +'<span><i class="red">￥'+$unitPrice+'</i></span>'
		                                 +'</div>'
		                                 +'<div>'+product.quantity+'</div>'
		                                 +'<div>'+order.memberName+'</div>'
		                                 +'</li>'
				                    +'</ul>';
			    $goods.append($listingStr);
			
		});	
	    var amount = order.totalAmount;
	    amount = parseFloat(amount).toFixed(2);
	    var shipMoney = order.shipMoney;
	    shipMoney = parseFloat(shipMoney).toFixed(2);
	    var $mul = '<div class="l mul" style="'+$height+'">'
	    	          +'<span class="price"><i class="red">￥'+amount+'</i></span><br />'
	    	          +'<span>(含运费：<i class="red">￥'+shipMoney+'<i>)</span>'
	    	      +'</div>'
	    	      +'<div class="l mul"  style="'+$height+'">'
	    	          +status
		          +'</div>'; 
	    $mul_.append($mul);
		var memo=order.memo?order.memo:'暂无备注';
		pmtGoods =(pmtGoods==null)?0:pmtGoods;
		pmtOrder =(pmtOrder==null)?0:pmtOrder;
		pmgGoods = parseFloat(pmtGoods).toFixed(2);
		pmtOrder = parseFloat(pmtOrder).toFixed(2);
		var shopPro  = CommnUtil.notNull(proDetailMap.proDetailTitle)?"店铺参与"+proDetailMap.proDetailTitle +"优惠活动，优惠了 <i class='red'>" + pmtOrder+"元</i>":"合计优惠了<i class='red'>"+pmtGoods+"元</i>";
		var $pStr ='买家备注：'+ memo +'<span class="shopPormotionMoney" style="margin-left:300px;">'+shopPro+'</span>' +'<span class="r">实付金额：<i class="red">￥'+amount+'</i></span>'	;
		$(".order_details .or_main .listing .note").append($pStr); 
	}else if(result.resultSuccess == false){
		alert("查询订单信息失败!")
	}
}
//商品促销优惠	
function goodsProMoney(totalPrice,goodsProMap){
	var result={};
	if(CommnUtil.notNull(goodsProMap)){
		var proType = goodsProMap.proType;
		if('0' == proType || '1' == proType ){ //订单满减或 商品满减
			result["proDetailTitle"] = goodsProMap.proDetailTitle;
			result["discountamount"] = goodsProMap.discountamount;
	    }else if('2' == proType){//商品满赠	
			result["proDetailTitle"] = goodsProMap.proDetailTitle;	
		}else if('3' == proType){//商品打折
			result["proDetailTitle"] = goodsProMap.proDetailTitle;
			 var $discountRate = goodsProMap.discountRate;
			 result["discountamount"]  = totalPrice*(1-$discountRate*1);		
		}else{
			return result;
		}
	}
	return result;
}

